import typing

from d_utils.db_entity import LawDataProduce, LawChineseDataProduce
from d_utils.db_init import DbInit


# 获取一条数据
def get_data_from_cloud_db() -> typing.Type[LawDataProduce] | None:
    DbInit.init_db()

    # 获取记录，满足中文字段是空、只取一条
    with (DbInit.g_session_local() as session):
        # 思路：
        # 1. 首先获取到LawChineseDataProduce中所有的law_data_produce_id的值的集合
        # 2. 查询 LawDataProduce中的记录的id不在 law_data_produce_id的值的集合中
        # 3. 只是获取一条记录

        # 1. 获取 LawChineseDataProduce 表中所有的 law_data_produce_id
        law_ids = session.query(LawChineseDataProduce.law_data_produce_id).all()
        law_id_set = {id_[0] for id_ in law_ids}

        # 2. 查询 LawDataProduce 表中 id 不在 law_id_set 中的记录
        law_data = session.query(LawDataProduce).filter(
            ~LawDataProduce.id.in_(law_id_set)
        ).limit(1).one_or_none()

        # 3. 返回查询结果（如果存在）
        if law_data is None:
            return None

        _id = law_data.id
        # todo(lzd)
        # need write the id to redis for mul process deal it
        return law_data


def begin_trans():
    entity: LawDataProduce | None = get_data_from_cloud_db()
    if entity is None:
        return




if __name__ == '__main__':



    pass
